package com.xzlcPT.controller;

import com.util.PageBean;
import com.xzlcPT.bean.*;
import com.xzlcPT.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/8.
 */
@Controller
@RequestMapping("xzMsg")
@SessionAttributes({"userLogin", "adminLogin"})
public class XzMsgController {
    @Autowired
    private XzMsgService msgService;
    @Autowired
    private XzCompanyService companyService;
    @Autowired
    private XzPostionService postionService;
    @Autowired
    private XzPostionSendMsgService postionSendMsgService;
    @Autowired
    private XzPostionSendService xzPostionSendService;
    @Autowired
    private LoginUserService loginService;

    @RequestMapping("selMsgAll.do")
    public ModelAndView selMsgAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows, @ModelAttribute("userLogin") XzLogin xzLogin) {
        ModelAndView mv = new ModelAndView("foreEnd3/messages");
        PageBean<XzMsg> pageBean = msgService.selectByReceiveId(page, rows, xzLogin.getLoginId());
        mv.addObject("msgList", pageBean.getList());
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("rows", pageBean.getPageSize());
        mv.addObject("total", pageBean.getTotal());
        return mv;
    }

    @RequestMapping("deleteById.do")
    public ModelAndView deleteById(Long msgId) {
        ModelAndView mv = new ModelAndView("redirect:selMsgAll.do");
        msgService.deleteById(msgId);
        return mv;
    }

    @RequestMapping("selMsgById.do")
    public ModelAndView selMsgById(Long msgId) {
        ModelAndView mv = new ModelAndView("foreEnd3/message_info");
        XzMsg xzMsg = msgService.updateToReadById(msgId);
        mv.addObject("xzMsg", xzMsg);
        return mv;
    }

    @ResponseBody
    @RequestMapping("deleteAll.do")
    public Map deleteAll(Long[] ids) {
        Map map = new HashMap();
        int i = msgService.deleteAll(ids);
        map.put("i", i);
        return map;
    }

    @ResponseBody
    @RequestMapping("noRaadCount.do")
    public Map conut(@ModelAttribute("userLogin") XzLogin xzLogin) {
        Map map = new HashMap();
        System.out.println(xzLogin.getLoginId());
        int i = msgService.noReadCount(xzLogin.getLoginId());
        System.out.println(i);
        map.put("count", i);
        return map;
    }

    //管理员审核成功职位发消息
    @ResponseBody
    @RequestMapping("postionMsg.emp")
    public Map postionMsg(@ModelAttribute("adaminLogin") XzLogin xzLogin, Long companyId, String postionName) {
        Map map = new HashMap();
        XzMsg xzMsg = new XzMsg();
        xzMsg.setMsgTital("你发布的职位有新进展");
        xzMsg.setMsgContent("您所发布的职位" + postionName + "通过了审核");
        xzMsg.setMsgSendId(xzLogin.getLoginId());
        xzMsg.setMsgSendName("管理员");
        XzCompany xzCompany = companyService.selectByPrimaryKey(companyId);
        xzMsg.setMsgReceiveId(xzCompany.getLoginId());
        xzMsg.setMsgReceiveName(xzCompany.getCompanyName());
        Date date = new Date();
        xzMsg.setMsgTime(date);
        xzMsg.setMsgType(5);
        xzMsg.setMsgIsread(0);
        xzMsg.setCreateTime(date);
        xzMsg.setOutId(xzLogin.getLoginId());
        int i = msgService.insertSelective(xzMsg);
        if (i == 1) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "err");
        }
        return map;
    }

    //管理员审核执照
    @ResponseBody
    @RequestMapping("companyMsg.emp")
    public Map companyMsg(@ModelAttribute("adminLogin") XzLogin xzLogin, Long companyId, String companyName) {
        Map map = new HashMap();
        XzMsg xzMsg = new XzMsg();
        xzMsg.setMsgTital("你提交的执照有新进展");
        xzMsg.setMsgContent("您" + companyName + "公司提交的执照通过了审核");
        xzMsg.setMsgSendId(xzLogin.getLoginId());
        xzMsg.setMsgSendName("管理员");
        XzCompany xzCompany = companyService.selectByPrimaryKey(companyId);
        xzMsg.setMsgReceiveId(xzCompany.getLoginId());
        xzMsg.setMsgReceiveName(xzCompany.getCompanyName());
        Date date = new Date();
        xzMsg.setMsgTime(date);
        xzMsg.setMsgType(5);
        xzMsg.setMsgIsread(0);
        xzMsg.setCreateTime(date);
        int i = msgService.insertSelective(xzMsg);
        if (i == 1) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "err");
        }
        return map;
    }

    //管理员审核执照失败发消息
    @ResponseBody
    @RequestMapping("filedCompanyMsg.emp")
    public Map filedCompanyMsg(@ModelAttribute("adminLogin") XzLogin xzLogin, String msgContent, Long companyId, String companyName) {
        Map map = new HashMap();
        XzMsg xzMsg = new XzMsg();
        xzMsg.setMsgTital("您" + companyName + "公司提交的执照未通过审核");
        xzMsg.setMsgContent(msgContent);
        xzMsg.setMsgSendId(xzLogin.getLoginId());
        xzMsg.setMsgSendName("管理员");
        XzCompany xzCompany = companyService.selectByPrimaryKey(companyId);
        xzMsg.setMsgReceiveId(xzCompany.getLoginId());
        xzMsg.setMsgReceiveName(xzCompany.getCompanyName());
        Date date = new Date();
        xzMsg.setMsgTime(date);
        xzMsg.setMsgType(5);
        xzMsg.setMsgIsread(0);
        xzMsg.setCreateTime(date);
        System.out.println(xzLogin);
        int i = msgService.insertSelective(xzMsg);
        xzCompany.setCompanyState(1);
        companyService.adoptCompany(xzCompany);
        if (i == 1) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "err");
        }
        return map;
    }

    //用户拒绝面试邀请发消息
    @ResponseBody
    @RequestMapping("refuseMsg.do")
    public Map refuseMsg(@ModelAttribute("userLogin") XzLogin xzLogin, String msgContent, Long pmsgId) {
        Map map = new HashMap();
        XzPostionSendMsg xzPostionSendMsg = postionSendMsgService.selByKey(pmsgId);
        XzMsg xzMsg = new XzMsg();
        xzMsg.setMsgTital(xzLogin.getMember().getMemberName() + "拒绝了面试邀请");
        xzMsg.setMsgContent(msgContent);
        xzMsg.setMsgSendId(xzLogin.getLoginId());
        xzMsg.setMsgSendName(xzLogin.getMember().getMemberName());
        XzPostionSend xzPostionSend = xzPostionSendService.selBykey(xzPostionSendMsg.getSendId());
        XzCompany xzCompany = companyService.selectByPrimaryKey(xzPostionSend.getCompanyId());
        xzMsg.setMsgReceiveId(xzCompany.getLoginId());
        xzMsg.setMsgReceiveName(xzCompany.getCompanyName());
        Date date = new Date();
        xzMsg.setMsgTime(date);
        xzMsg.setMsgType(4);
        xzMsg.setMsgIsread(0);
        xzMsg.setCreateTime(date);
        System.out.println(xzLogin);
        int i = msgService.insertSelective(xzMsg);
        xzCompany.setCompanyState(1);
        companyService.adoptCompany(xzCompany);
        if (i == 1) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "err");
        }
        return map;
    }

    //用户同意面试邀请发消息
    @ResponseBody
    @RequestMapping("agreeMsg.do")
    public Map agreeMsg(@ModelAttribute("userLogin") XzLogin xzLogin, Long pmsgId) {
        Map map = new HashMap();
        XzPostionSendMsg xzPostionSendMsg = postionSendMsgService.selByKey(pmsgId);
        XzMsg xzMsg = new XzMsg();
        xzMsg.setMsgTital(xzLogin.getMember().getMemberName() + "同意了面试邀请");
        xzMsg.setMsgContent("同意面试邀请");
        xzMsg.setMsgSendId(xzLogin.getLoginId());
        xzMsg.setMsgSendName(xzLogin.getMember().getMemberName());
        XzPostionSend xzPostionSend = xzPostionSendService.selBykey(xzPostionSendMsg.getSendId());
        XzCompany xzCompany = companyService.selectByPrimaryKey(xzPostionSend.getCompanyId());
        xzMsg.setMsgReceiveId(xzCompany.getLoginId());
        xzMsg.setMsgReceiveName(xzCompany.getCompanyName());
        Date date = new Date();
        xzMsg.setMsgTime(date);
        xzMsg.setMsgType(4);
        xzMsg.setMsgIsread(0);
        xzMsg.setCreateTime(date);
        System.out.println(xzLogin);
        int i = msgService.insertSelective(xzMsg);
        xzCompany.setCompanyState(1);
        companyService.adoptCompany(xzCompany);
        if (i == 1) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "err");
        }
        return map;
    }

    //后台管理员操作--------------------------
    //后台首页查询所有
    @RequestMapping("selAllMsg.emp")
    public ModelAndView selAllMsg(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows, XzMsg xzMsg) {
        ModelAndView mv = new ModelAndView("backEnd3/msg_index");
        PageBean<XzMsg> pageBean = msgService.selAllMsg(page, rows, xzMsg);
        mv.addObject("xzMsg", xzMsg);
        mv.addObject("list", pageBean.getList());
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("total", pageBean.getTotal());
        return mv;
    }

    //后台按msgId查询
    @RequestMapping("selectByMsgId.emp")
    public ModelAndView selectByMsgId(long msgId, int a) {
        ModelAndView mv = new ModelAndView("/backEnd3/msg_revise");
        XzMsg xzMsg = msgService.selMsgById(msgId);
        if (a == 2) {
            mv.setViewName("/backEnd3/msg_updata");
        }
        mv.addObject("msg", xzMsg);
        return mv;
    }

    // 后台管理员修改
    @RequestMapping("updateMsgByEmp.emp")
    public ModelAndView updateMsgByEmp(XzMsg xzMsg) {
        ModelAndView mv = new ModelAndView("redirect:selAllMsg.emp");
        Map map = new HashMap();
        int i = msgService.updateMsgByEmp(xzMsg);
        if (i == 1) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "err");
        }
        mv.addObject("map", map);
        return mv;
    }

    //移动端方法**********************************************************************
    //  我的消息
    @ResponseBody
    @RequestMapping("selMsgAllMob.go")
    public Map selMsgAll_Mob(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows, @RequestHeader("accessToken") String token) {
        XzLogin xzLogin = loginService.selByToken(token);
        Map map = new HashMap();
        PageBean<XzMsg> pageBean = msgService.selectByReceiveId(page, rows, xzLogin.getLoginId());
        map.put("msgList", pageBean.getList());
        map.put("page", pageBean.getPageNum());
        map.put("pages", pageBean.getPages());
        map.put("rows", pageBean.getPageSize());
        map.put("total", pageBean.getTotal());
        return map;
    }

    //删除我的消息
    @ResponseBody
    @RequestMapping("deleteByIdMob.go")
    public Map deleteByIdMob(Long msgId) {
        Map map = new HashMap();
        int i = msgService.deleteById(msgId);
        if (i == 1) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "err");
        }
        return map;
    }

    //消息设为已读
    @ResponseBody
    @RequestMapping("isReadByIdMob.go")
    public Map isReadByIdMob(Long msgId) {
        Map map = new HashMap();
        int i = msgService.updateIsReadById(msgId);
        if (i == -1) {
            map.put("msg", "noChange");
        } else if (i > 0) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "err");
        }
        return map;
    }

    //20171216-王继顺
    //移动端-企业首页消息
    @ResponseBody
    @RequestMapping("selComMsgAllMob.go")
    public Map selMsgAllComMob(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows, @RequestHeader("accessToken") String token) {
        XzLogin xzLogin = loginService.selByToken(token);
        PageBean<XzMsg> pageBean = msgService.selectByReceiveId(page, rows, xzLogin.getCompany().getCompanyId());
        Map<String, Object> map = new HashMap<>();
        map.put("msgList", pageBean.getList());
        map.put("page", pageBean.getPageNum());
        map.put("pages", pageBean.getPages());
        map.put("rows", pageBean.getPageSize());
        map.put("total", pageBean.getTotal());
        return map;
    }

    //20171226-王继顺
    //移动端-企业-未读消息数
    @ResponseBody
    @RequestMapping("noReadCountE.go")
    public Map noReadCountE(@RequestHeader("accessToken") String token) {
        XzLogin xzLogin = loginService.selByToken(token);
        int i = msgService.noReadCount(xzLogin.getCompany().getCompanyId());
        Map<String, Integer> map = new HashMap<>();
        map.put("count", i);
        return map;
    }

    //20171226-王继顺
    //移动端-个人-未读消息数
    @ResponseBody
    @RequestMapping("noReadCount.go")
    public Map noReadCount(@RequestHeader("accessToken") String token) {
        XzLogin xzLogin = loginService.selByToken(token);
        int i = msgService.noReadCount(xzLogin.getLoginId());
        Map<String, Integer> map = new HashMap<>();
        map.put("count", i);
        return map;
    }

}
